package org.csu.mypetstore.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.csu.mypetstore.api.common.CommonResponse;
import org.csu.mypetstore.api.entity.Account;
import org.csu.mypetstore.api.entity.BannerData;
import org.csu.mypetstore.api.entity.Profile;
import org.csu.mypetstore.api.entity.SignOn;
import org.csu.mypetstore.api.persistence.AccountMapper;
import org.csu.mypetstore.api.persistence.BannerDataMapper;
import org.csu.mypetstore.api.persistence.ProfileMapper;
import org.csu.mypetstore.api.persistence.SignOnMapper;
import org.csu.mypetstore.api.service.AccountService;
import org.csu.mypetstore.api.vo.AccountVO;
import org.csu.mypetstore.api.vo.ItemVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("accountService")
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountMapper accountMapper;

    @Autowired
    private BannerDataMapper bannerDataMapper;

    @Autowired
    private ProfileMapper profileMapper;

    @Autowired
    private SignOnMapper signOnMapper;

    @Override
    public CommonResponse<AccountVO> getAccount(String username, String password) {
        QueryWrapper<SignOn> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        queryWrapper.eq("password",password);
        SignOn signOn = signOnMapper.selectOne(queryWrapper);
        //SignOn signOn = signOnMapper.selectOne(queryWrapper);
        if(signOn == null){
            return CommonResponse.createForError("用户名或密码不正确");
        }
        return getAccount(username);
    }

    @Override
    public CommonResponse<AccountVO> getAccount(String username) {
        Account account = accountMapper.selectById(username);
        Profile profile = profileMapper.selectById(username);
        BannerData bannerData = bannerDataMapper.selectById(profile.getFavcategory());
        if(account == null){
            return CommonResponse.createForError("获取用户信息失败");
        }
            AccountVO accountVO = entityToVO(account,profile,bannerData);
        return CommonResponse.createForSuccess(accountVO);

    }

    public CommonResponse<AccountVO> getUserDetails(String username) {
        Account account = accountMapper.selectById(username);
        Profile profile = profileMapper.selectById(username);
        BannerData bannerData = bannerDataMapper.selectById(profile.getFavcategory());
        if(account == null){
            return CommonResponse.createForError("获取用户信息失败");
        }
        AccountVO accountVO = entityToVO(account,profile,bannerData);
        return CommonResponse.createForSuccess(accountVO);

    }

    private AccountVO entityToVO(Account account,Profile profile,BannerData bannerData){
        AccountVO accountVO = new AccountVO();
        accountVO.setUsername(account.getUsername());
        accountVO.setPassword("");
        accountVO.setEmail(account.getEmail());
        accountVO.setFirstname(account.getFirstname());
        accountVO.setLastname(account.getLastname());
        accountVO.setState(account.getState());
        accountVO.setAddr1(account.getAddr1());
        accountVO.setAddr2(account.getAddr2());
        accountVO.setCity(account.getCity());
        accountVO.setStatus(account.getStatus());
        accountVO.setCountry(account.getCountry());
        accountVO.setZip(account.getZip());
        accountVO.setPhone(account.getPhone());

        accountVO.setLangpref(profile.getLangpref());
        accountVO.setBanneropt(profile.getBanneropt() == 1);
        accountVO.setMylistopt(profile.getMylistopt() == 1);
        if(profile.getBanneropt() == 1){
            accountVO.setFavcategory(profile.getFavcategory());
            accountVO.setBannername(bannerData.getBannername());
        }else {
            accountVO.setFavcategory("");
            accountVO.setBannername("");
        }
        return accountVO;
    }

   @Override
    public CommonResponse<AccountVO> getUpdataPassword(String username, String password) {
        SignOn signOn = signOnMapper.selectById(username);
        signOn.setPassword(password);
        signOnMapper.updateById(signOn);
        Account account = accountMapper.selectById(username);
        Profile profile = profileMapper.selectById(username);
        BannerData bannerData = bannerDataMapper.selectById(profile.getFavcategory());
        if(account == null){
            return CommonResponse.createForError("获取用户信息失败");
        }
        AccountVO accountVO = entityToVO(account,profile,bannerData);
        return CommonResponse.createForSuccess(accountVO);

    }

    @Override
    public CommonResponse<AccountVO> getUpdataUserAll(String username, String password) {
        return null;
    }

    @Override
    public CommonResponse<AccountVO> getUpdataUserAll(AccountVO accountVO) {
        if(accountVO==null){
            return CommonResponse.createForError("修改为空");
        }
        if(accountVO.getUsername()==null){
            return CommonResponse.createForError("用户名不能为空");
        }
        Account account = accountMapper.selectById(accountVO.getUsername());
        if(account == null){
            return CommonResponse.createForError("获取用户信息失败");
        }
        SignOn signOn = signOnMapper.selectById(accountVO.getUsername());
        signOn.setUsername(accountVO.getUsername());
        if(accountVO.getPassword()==null){
            return CommonResponse.createForError("密码不能为空");
        }
        signOn.setPassword(accountVO.getPassword());

        signOnMapper.updateById(signOn);

        account.setUsername(accountVO.getUsername());
        account.setEmail(accountVO.getEmail());
        account.setFirstname(accountVO.getFirstname());
        account.setLastname(accountVO.getLastname());
        account.setStatus(accountVO.getStatus());
        account.setAddr1(accountVO.getAddr1());
        account.setAddr2(accountVO.getAddr2());
        account.setCity(accountVO.getCity());
        account.setState(accountVO.getState());
        account.setZip(accountVO.getZip());
        account.setCountry(accountVO.getCountry());
        account.setPhone(accountVO.getPhone());
        Profile profile = profileMapper.selectById(accountVO.getUsername());
        profile.setFavcategory(accountVO.getFavcategory());
        profile.setBanneropt(accountVO.isBanneropt()? 1 :0);
        profile.setUsername(accountVO.getUsername());
        profile.setMylistopt(accountVO.isMylistopt()? 1 :0);
        profile.setLangpref(accountVO.getLangpref());
        BannerData bannerData = bannerDataMapper.selectById(profile.getFavcategory());
        bannerData.setBannername(accountVO.getBannername());
        bannerData.setFavouciteCategoryId(accountVO.getFavouciteCategoryId());
        accountMapper.updateById(account);
        profileMapper.updateById(profile);
        signOnMapper.updateById(signOn);
        bannerDataMapper.updateById(bannerData);
        return CommonResponse.createForSuccess(accountVO);

    }

    @Override
    public CommonResponse<AccountVO> getRegister(AccountVO accountVO) {
        if(accountVO==null){
            return CommonResponse.createForError("创建不能为空");
        }
        if(accountVO.getUsername()==null){
            return CommonResponse.createForError("用户名不能为空");
        }
        Account account = new Account();
        SignOn signOn = new SignOn();
        signOn.setUsername(accountVO.getUsername());
        if(accountVO.getPassword()==null){
            return CommonResponse.createForError("密码不能为空");
        }
        signOn.setPassword(accountVO.getPassword());
        account.setUsername(accountVO.getUsername());
        account.setEmail(accountVO.getEmail());
        account.setFirstname(accountVO.getFirstname());
        account.setLastname(accountVO.getLastname());
        account.setStatus(accountVO.getStatus());
        account.setAddr1(accountVO.getAddr1());
        account.setAddr2(accountVO.getAddr2());
        account.setCity(accountVO.getCity());
        account.setState(accountVO.getState());
        account.setZip(accountVO.getZip());
        account.setCountry(accountVO.getCountry());
        account.setPhone(accountVO.getPhone());
        Profile profile = new Profile();
        profile.setFavcategory(accountVO.getFavcategory());
        profile.setBanneropt(accountVO.isBanneropt()? 1 :0);
        profile.setUsername(accountVO.getUsername());
        profile.setMylistopt(accountVO.isMylistopt()? 1 :0);
        profile.setLangpref(accountVO.getLangpref());
        BannerData bannerData = new BannerData();
        bannerData.setBannername(accountVO.getBannername());
        bannerData.setFavouciteCategoryId(accountVO.getFavouciteCategoryId());
        accountMapper.insert(account);
        profileMapper.insert(profile);
        signOnMapper.insert(signOn);
        bannerDataMapper.insert(bannerData);
        return CommonResponse.createForSuccess(accountVO);
    }

}
